//
//  YSLRecommendCell.m
//  used
//
//  Created by lanou on 16/7/14.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "YSLRecommendCell.h"

@interface YSLRecommendCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)NSArray *collectionArray;

@property (weak, nonatomic) IBOutlet UIImageView *userimge; //头像

@property (weak, nonatomic) IBOutlet UILabel *sallername; //昵称

@property (weak, nonatomic) IBOutlet UILabel *originprice; //原价

@property (weak, nonatomic) IBOutlet UILabel *saleprice; //现价

@property (weak, nonatomic) IBOutlet UICollectionView *imageurls; //商品图片

@property (weak, nonatomic) IBOutlet UILabel *desc; //商品信息

@property (weak, nonatomic) IBOutlet UILabel *city; //城市名字

@property (weak, nonatomic) IBOutlet UILabel *area; //城市区域

@property (weak, nonatomic) IBOutlet UILabel *likenum; //点赞

@property (weak, nonatomic) IBOutlet UILabel *readcount; //阅读

@property (weak, nonatomic) IBOutlet UILabel *commentscount; //评论





@end

@implementation YSLRecommendCell




- (IBAction)clickButton:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(clickButton:withModel:)]) {
        [self.delegate clickButton:sender withModel:self.model];
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
  
    self.imageurls.dataSource = self;
    self.imageurls.delegate = self;
    [self.imageurls registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell_cell"];
    
    [self.imageurls addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActoon)]];
    
    
}


- (void)tapActoon {
    
    if ([_delegate respondsToSelector:@selector(clickCollectionViewWithModelId:)]) {
        [self.delegate clickCollectionViewWithModelId:self.model.ID];
    }
}


#pragma mark -collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell_cell" forIndexPath:indexPath];

    //设置cell
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.collectionArray[indexPath.row]]];
    return cell;
}
- (void)setModel:(KLGood *)model {
    _model = model;
    

    self.originprice.text = model.originprice.description; //原价
    self.saleprice.text = model.saleprice.description; //现价
    [self.userimge sd_setImageWithURL:[NSURL URLWithString:model.userimge]]; //头像
    self.sallername.text = model.sallername; //昵称
    self.city.text = model.city; //城市
    self.area.text = model.area; //区域
    self.desc.text = model.desc; //商品信息
    
//    self.readcount.text = model.readcount.description;
    
    if (model.commentscount) {
        
        self.commentscount.text = model.commentscount.description;
    }else {
        self.commentscount.text = 0;
    }
    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        BmobQuery *query = [BmobQuery queryWithClassName:@"Zan"];
        [query whereKey:@"desc" equalTo:self.model.desc];
        [query whereKey:@"productname" equalTo:self.model.goodsname];
        [query orderByDescending:@"updatedAt"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            NSInteger count = [model.likenum integerValue];
            count = count + array.count;
            self.likenum.text = [NSString stringWithFormat:@"%ld",count];
        }];
        
        BmobQuery *readCount = [BmobQuery queryWithClassName:@"ReadCount"];
        [readCount whereKey:@"desc" equalTo:self.model.desc];
        [readCount whereKey:@"productname" equalTo:self.model.goodsname];
        [readCount orderByDescending:@"updatedAt"];
        [readCount findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            self.readcount.text = [NSString stringWithFormat:@"%ld", array.count];
        }];

    });
    
    self.collectionArray = model.imageurls;
    [self.imageurls reloadData];
    
}


@end
